<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>百慧智慧wms仓库</title>
  <link href="../layui/css/layui.css" rel="stylesheet">
</head>
<body>

    <div class="layui-container">
        <!--菜单数据表格-->
        <table id="menuId" lay-filter="test"></table>
    </div>


<script src="../layui/layui.js"></script>
<script>
    layui.extend({"treeTable":"../js/treeTable"}).use(["treeTable","jquery","layer"],function () {
        //加载树形表格模块
        let treeTable = layui.treeTable;
        let $ = layui.jquery;
        let layer = layui.layer;

        //表的列事件绑定
        treeTable.on("tool(menuId)",function (obj) {
            let e = obj.event;
            if(e==="del"){
                layer.confirm("是否删除",function (index){
                    layer.close(index);//关闭弹出层
                    $.getJSON("/deleteMenu",{menuId:obj.data.menuId},function (d){
                        if(d.code!=0){
                            layer.msg(d.msg);
                        }else{
                            //刷新数据表格
                            treeTable.reload("menuId");
                        }
                    });
                });
            }else if(e==="edit"){

                layer.open({
                    type:2,
                    title:'更新菜单',
                    content:'menusUpdate.html?isDelete='+obj.data.isDelete,
                    area:['500px','450px'],
                    success: function(layero, index){
                        //弹出执行
                        let body = layer.getChildFrame('body', index);//获取弹出层中body标签的DOM对象
                        //给弹出层表单回显
                        body.find('#menuId').val(obj.data.menuId);
                        body.find('#parentId').val(obj.data.parentId);
                        body.find('#level').val(obj.data.level);
                        body.find('#menuName').val(obj.data.menuName);
                        body.find('#menuUrl').val(obj.data.menuUrl);
                        body.find('#createTime').val(obj.data.createTime);
                    },
                    end:function (){
                        //刷新数据表格
                        treeTable.reload("menuId");
                    }
                });

            }else if(e==="add"){
                layer.open({
                    type:2,
                    title:'添加菜单',
                    content:'menusAdd.html',
                    area:['500px','450px'],
                    success: function(layero, index){
                        //弹出执行
                        let body = layer.getChildFrame('body', index);//获取弹出层中body标签的DOM对象
                        //给弹出层表单回显
                        body.find('#parentId').val(obj.data.menuId);
                        body.find('#level').val(2);
                    },
                    end:function (){
                        //刷新数据表格
                        treeTable.reload("menuId");
                    }
                });
            }
        });

        //表头事件绑定
        treeTable.on("toolbar(menuId)",function (obj){
            let e = obj.event;
            if(e==="addMenu"){
                layer.open({
                    type:2,
                    title:'添加菜单',
                    content:'menusAdd.html',
                    area:['500px','450px'],
                    success: function(layero, index){
                        //弹出执行
                        let body = layer.getChildFrame('body', index);//获取弹出层中body标签的DOM对象
                        //给弹出层表单回显
                        body.find('#parentId').val(0);
                        body.find('#level').val(1);
                    },
                    end:function (){
                        //刷新数据表格
                        treeTable.reload("menuId");
                    }
                });

            }else {
                layer.msg("功能还在路上");
            }
        });


        //初始化数据表格
        treeTable.render({
            elem: '#menuId',
            url: '/getMenuList',
            toolbar: '<div><button lay-event="addMenu" class="layui-btn layui-btn-sm"> <i class="layui-icon layui-icon-add-1"></i>添加一级菜单</button> </div>',
            defaultToolbar: ['exports', 'print'],
            tree: {
                iconIndex: 0,
                isPidData: true,
                idName: 'menuId',
                pidName: 'parentId'
            },
            parseData:function(res){
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "data": res.obj
                }
            },
            cols:[[
                {
                    field: 'menuName',
                    title: '菜单名称',
                    width: '35%'
                },
                {
                    field: 'level',
                    title: '层级',
                    width: '15%',
                    templet:function (d){
                        return '<span class="layui-badge layui-bg-'+(d.level==1?"blue":"green")+'">'+d.level+'</span>';
                    }
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {
                    field: 'updateTime',
                    title: '更新时间'
                },
                {
                    field: 'isDelete',
                    title: '是否有效',
                    width: '15%',
                    templet:function (d){
                        return d.isDelete==1?'<span class="layui-badge layui-bg-green">有效</span>':'<span class="layui-badge">无效</span>';
                    }
                },
                {
                    title: '操作',
                    templet:function (d){
                        let s='<button type="button" lay-event="del" class="layui-btn layui-btn-xs layui-btn-danger">删除</button>';
                        s+='<button type="button" lay-event="edit" class="layui-btn layui-btn-xs layui-btn-warm">更新</button>';
                        if(d.level!=2){
                            s+='<button type="button" lay-event="add" class="layui-btn layui-btn-xs layui-btn-normal">添加</button>';
                        }
                        return s;
                    }
                }
            ]]
        });


    });
</script>
</body>
</html>